<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class km_barang_supplier extends MY_Controller {
	
	function __construct()
	{
		parent::__construct(); 
		
		$this->load->model('km_barang_supplier_model', 'BSupplier');
		$this->load->model('combo_model', 'Combo');
		$this->secure_session();
	}
	
	function index(){
		$data['title'] = "Setup Barang Supplier";
		
		// Define Combo Data
		$data['arr_application']	= $this->Combo->ITApplication();
		
		viewByTemplate('km_barang_supplier/index', $data, 'grid');
	}
	
	function getdata()
	{
		$this->secure_session();
		echo $this->BSupplier->GetJSON();
	}

	private function validation()
	{
		$error = new ErrorList();

		// Get Error Template
		$tpl_empty = $this->MessageTemplate("ER001");
		$tpl_duplicate = $this->MessageTemplate("ER002");


		//bs_id, supplier_id, barang_id, bs_date_begin, bs_date_end, bs_nilai, bs_status, bs_return
		if (post("supplier_id") == '') {
			$error->Add("supplier_id", "Supplier", $tpl_empty);
		} else if (post("barang_id") == '') {
			$error->Add("barang_id", "Barang", $tpl_empty);
		} else if (post("bs_status") == '') {
			$error->Add("bs_status", "Status", $tpl_empty);
		} else if (post("bs_return") == '') {
			$error->Add("bs_return", "Barang Return", $tpl_empty);
		} else if (post("mode") == "create" && intval($this->get_data("SELECT 1 FROM km_barang_supplier a JOIN km_supplier b on  a.supplier_id=b.supplier_id JOIN km_barang c on a.barang_id=c.barang_id WHERE supplier_id = '".@ereg_replace("[^a-zA-Z0-9_-]", "",@strtoupper(@trim(post("supplier_id"))))."' AND barang_id = '".@ereg_replace("[^a-zA-Z0-9_-]", "",@strtoupper(@trim(post("barang_id"))))."'" )) > 0) {
			$error->Add("bs_id", "Barang Supplier", $tpl_duplicate);
		}		
		return $error;
	}
	
	function ajax_post()
	{
		$is_error = 0;
		$ls_response = "";
		$ls_genid = "";

		$error = $this->validation();
		try
		{
			if (count( $error->Get() ) > 0) { $is_error = 1; }
			else
			{
				
				
				if (post("mode") == "create")
				{
					// Fill Post Data
					$data	= post_by_key(array( 'supplier_id', 'barang_id', 'bs_date_begin', 'bs_date_end', 'bs_nilai', 'bs_status', 'bs_return'));
					$data['bs_status']	= @ereg_replace("[^a-zA-Z0-9_-]", "",@strtoupper(@trim($data['bs_status'])));
					$data['bs_return']	= @ereg_replace("[^a-zA-Z0-9_-]", "",@strtoupper(@trim($data['bs_return'])));
					$this->BSupplier->Add($data);
					$ls_response = "Data Tersimpan";

				}
				else if (post("mode") == "edit")
				{
					// Fill Post Data
					$data	= post_by_key(array('bs_id','supplier_id', 'barang_id', 'bs_date_begin', 'bs_date_end', 'bs_nilai', 'bs_status', 'bs_return'));
					$data['bs_status']	= @ereg_replace("[^a-zA-Z0-9_-]", "",@strtoupper(@trim($data['bs_status'])));
					$data['bs_return']	= @ereg_replace("[^a-zA-Z0-9_-]", "",@strtoupper(@trim($data['bs_return'])));
					$this->BSupplier->Update($data, array('bs_id'	=> $data['bs_id']));
					$ls_response = "Data Tersimpan";
				}
			}
		} catch(Exception $e) {
			$is_error = 1; $ls_response = $e->getMessage();
		}
		
		echo $this->PostResponse(json_encode(array(	'is_error'	=> $is_error,
													'fields'	=> $error->GetDictionary(),
													'msg'		=> $ls_response,
													'genid'		=> $ls_genid,
													'form_sender'=> post("form_sender"))
								));
	}

	function ajax_delete()
	{
		$ls_result	= "";
		$bs_id	= get('bs_id');
		
		if ($bs_id != "")
		{
			// Delete Single
			$arr_rule	= array('bs_id' => $bs_id);
			$ls_result = $this->BSupplier->DeleteSingle($arr_rule);

		}
		else
		{
			
			$num_row = intval($this->data['num_row']['deleted']);
			
			for ($i = 0; $i < $num_row; $i++)
			{
				$row	= $this->data['deleted'][$i];
				
				$arr_rule	= array('bs_id' => $row['bs_id']);
				$ls_result .= $this->BSupplier->DeleteSingle($arr_rule);
			}
		}
		
		echo $ls_result;
	}
}
?>